<template>
  <!-- <div
    class=""
    :class="{'border': annotated, 'border-danger': annotated}"
    @mouseover="hover = true"
    @mouseleave="hover = false"
  >
    <div @click="openAnnotator" class="img-container">
      <v-lazy-image
        :src="imageUrl"
        :src-placeholder="loaderUrl"
        class="card-img"
        style="width: 100%; display: block"
        :style="{'opacity': annotated ? 0.3 : 1}"
      />
      <button class="delete-button">
        <div class="sign">
          <svg
            viewBox="0 0 16 16"
            class="bi bi-trash3-fill"
            fill="currentColor"
            height="18"
            width="18"
            xmlns="http://www.w3.org/2000/svg"
          >
            <path
              d="M11 1.5v1h3.5a.5.5 0 0 1 0 1h-.538l-.853 10.66A2 2 0 0 1 11.115 16h-6.23a2 2 0 0 1-1.994-1.84L2.038 3.5H1.5a.5.5 0 0 1 0-1H5v-1A1.5 1.5 0 0 1 6.5 0h3A1.5 1.5 0 0 1 11 1.5m-5 0v1h4v-1a.5.5 0 0 0-.5-.5h-3a.5.5 0 0 0-.5.5M4.5 5.029l.5 8.5a.5.5 0 1 0 .998-.06l-.5-8.5a.5.5 0 1 0-.998.06Zm6.53-.528a.5.5 0 0 0-.528.47l-.5 8.5a.5.5 0 0 0 .998.058l.5-8.5a.5.5 0 0 0-.47-.528ZM8 4.5a.5.5 0 0 0-.5.5v8.5a.5.5 0 0 0 1 0V5a.5.5 0 0 0-.5-.5"
            ></path>
          </svg>
        </div>
        <div class="text">Delete</div>
      </button>
    </div>

    <div class="card-body" style="width: 100%" :style="{'opacity': annotated ? 0.3 : 1}">
      <div class="" style="width: 100%">
        <span
          :class="{ 'text-truncate': !hover }"
          style="width: 100%; float: left"
        >
          <strong class="card-title">
            {{ image.id }}:{{ image.file_name }}
          </strong>
        </span>
        <div
          class="dropdown-menu"
          :aria-labelledby="'dropdownImage' + image.id"
        >
          <button
            class="btn dropdown-item"
            @click="onDeleteClick"
          >
            删除
          </button>
          <button class="btn dropdown-item" @click="openAnnotator">
            进入标注
          </button>
        </div>
      </div>
    </div>
  </div> -->
  <section id="card1" class="card">
    <v-lazy-image 
      :src="imageUrl"
      :src-placeholder="loaderUrl"
      class="card-img"
      style="width: 100%; display: block"
      :style="{'opacity': annotated ? 0.3 : 1}"
    />
    <div class="card__content">
      <p class="card__title">{{ image.file_name }}</p>
      <div class="card__description">
        <div v-show="image.annotated == true && image.num_annotations > 0">Annotated</div>
        <div v-show="image.annotated == false">No annotations</div>
      </div>
      <button
        class="btn dropdown-item"
        @click="openAnnotator"
      >
        开始标注
      </button>
      <button
        class="btn dropdown-item"
        @click="onDeleteClick"
      >
        删除
      </button>
    </div>
  </section>
</template>

<script>
import axios from "axios";

export default {
  name: "ImageCard",
  props: {
    image: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      hover: false,
      showAnnotations: this.image.annotated,
      loaderUrl: require("@/assets/loader.gif"),
    };
  },
  methods: {
    downloadURI(uri, exportName) {
      let link = document.createElement("a");
      link.href = uri;
      link.download = exportName;
      document.body.appendChild(link);
      link.click();
      link.remove();
    },
    openAnnotator() {
      this.$router.push({
        name: "annotate",
        params: { identifier: this.image.id },
      });
    },
    onDownloadClick() {
      this.downloadURI(
        "/api/image/" + this.image.id + "?asAttachment=true",
        this.image.file_name
      );

      axios.get("/api/image/" + this.image.id + "/coco").then((reponse) => {
        let dataStr =
          "data:text/json;charset=utf-8," +
          encodeURIComponent(JSON.stringify(reponse.data));
        this.downloadURI(
          dataStr,
          this.image.file_name.replace(/\.[^/.]+$/, "") + ".json"
        );
      });
    },
    onDeleteClick() {
      axios.delete("/api/image/" + this.image.id).then(() => {
        this.$parent.updatePage();
      });
    },
  },
  computed: {
    // imageUrl() {
    //   return `/api/image/${this.image.id}`;
    // },
    imageUrl() {
      let d = new Date();
      if (this.showAnnotations) {
        return `/api/image/${
          this.image.id
        }?width=250&thumbnail=true&dummy=${d.getTime()}`;
      } else {
        return "/api/image/" + this.image.id + "?width=250";
      }
    },
    annotated() {
      if (!this.image.annotating) return 0;
      return this.image.annotating.length > 0;
    },
  },
};
</script>

<style scoped>
.img-container {
  overflow: hidden;
  border-radius: 5px;
  position: relative;
  overflow: hidden;
}
.img-container:hover {
}
.card-img {
  height: 400px;
  width: 600px;
  object-fit: cover;
  transition: all 0.5s ease-in-out;
}
.img-container:hover .card-img {
  /* transform: scale(1.05); */
  cursor: pointer;
}
.img-container:hover .delete-button {
  /* display: flex; */
}
/* 按钮们 */
.delete-button {
  position: absolute;
  transform: translateY(-100%);
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 45px;
  height: 45px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition-duration: 0.3s;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.199);
  background: rgb(255, 135, 65);
  background: linear-gradient(
    250deg,
    rgba(255, 135, 65, 1) 15%,
    rgba(255, 65, 65, 1) 65%
  );
  display: none;
}

/* plus sign */
.sign {
  width: 100%;
  transition-duration: 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sign svg {
  width: 17px;
}

.sign svg path {
  fill: white;
}
/* text */
.text {
  position: absolute;
  right: 0%;
  width: 0%;
  opacity: 0;
  color: white;
  font-size: 1.2em;
  font-weight: 600;
  transition-duration: 0.3s;
}
/* hover effect on button width */
.delete-button:hover {
  width: 125px;
  border-radius: 40px;
  transition-duration: 0.3s;
}

.delete-button:hover .sign {
  width: 30%;
  transition-duration: 0.3s;
  padding-left: 20px;
}
/* hover effect button's text */
.delete-button:hover .text {
  opacity: 1;
  width: 70%;
  transition-duration: 0.3s;
  padding-right: 10px;
}
/* button click effect*/
.delete-button:active {
  transform: translate(2px, 2px);
}

.overlay-text {
  position: absolute;
  padding: 10px;
  top: 30px;
  width: 100%;
}

.card-body {
  margin: 5px 5px 0 5px;
  padding: 10px 20px;
}

p {
  margin: 0;
  padding: 0 0 3px 0;
}

.list-group-item {
  height: 21px;
  font-size: 13px;
  padding: 2px;
  background-color: #4b5162;
}
.icon-more {
  position: absolute;
  right: 5px;
  padding: 3px 10px;
  float: right;
  color: black;
}
.category-badge {
  float: left;
  margin: 0 2px 5px 0;
}

/* 超级特效 */
section.card {
  position: relative;
  width: 300px;
  height: 300px;
  background-color: #f2f2f2;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  perspective: 1000px;
  transition: all 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.card svg {
  fill: #000;
  transition: all 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  border-radius: 3px;
}
.card:hover {
  transform: scale(1.05);
  box-shadow: 0 8px 16px #000000;
  background-color: #f2f2f2;
  color: #ffffff;
}

.card__content {
  color: #000000;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  background-color: #f2f2f2;
  transform: rotateX(-90deg);
  transform-origin: bottom;
  transition: all 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.card:hover .card__content {
  transform: rotateX(0deg);
}

.card__title {
  margin: 0;
  padding-left: 5px;
  font-size: 24px;
  color: #000000;
  font-weight: 700;
}

.card:hover svg {
  scale: 0;
}

.card__description {
  margin: 10px 0 0;
  font-size: 14px;
  color: #000000;
  line-height: 1.4;
}
/* Commands to change the shadows in dark mode
@media (prefers-color-scheme: dark) {
  .card:hover {
  box-shadow: 0 8px 16px #000000;
  }
}*/
</style>
